﻿using System;
using System.Collections.Generic;
using DatabaseStuff;

namespace DatabaseStuff.Basic
{
    /// <summary>
    /// Represents a basic implementation of a database
    /// </summary>
    public sealed class Database: IDatabase
    {
        private class TableSet : HashMap<string, Table>
        {
            public TableSet()
                : base()
            { }

            public TableSet(IDictionary<string, Table> _dict)
                : base(_dict)
            { }

            protected override string GetKeyFromValue(Table value)
            {
                return value.Name;
            }
        }

        private TableSet tables;

        public Database()
        {
            tables = new TableSet();
        }

        /// <summary>
        /// DI-Compatible Constructor
        /// </summary>
        /// <param name="_dict"></param>
        internal Database(IDictionary<string, Table> _dict)
        {
            tables = new TableSet(_dict);
        }

        /// <summary>
        /// Retrieve the number of tables currently referenced by this Database instance
        /// </summary>
        public int NumTables
        {
            get { return tables.Count; }
        }

        /// <summary>
        /// Retrieve the IDBTables loaded into this Database instance
        /// </summary>
        IEnumerable<IDBTable> DatabaseStuff.IDatabase.GetTables()
        {
            ICollection<IDBTable> result = new HashSet<IDBTable>();
            foreach (Table t in (ICollection<Table>)tables)
                result.Add(t);

            return result;

            //return (ICollection<IDBTable>)GetTables();
        }

        public ICollection<Table> GetTables()
        {
            return tables;
        }
    }
}
